﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Liang.DataBase.Model;
using Liang.UI;
using Liang.UI.Tools;
using WeifenLuo.WinFormsUI.Docking;
using Liang.DataBase.Model.Entity;
using Liang.DataBase.Tools;
using Liang.DataBase.BLL;
using Liang.Model;

namespace Client
{
    public partial class FrmUser : DockContent
    {
        private bool query = false;

        public FrmUser()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化dataGridView
        /// </summary>
        public void initView()
        {
            dataView.AutoGenerateColumns = false;
            dataView.Columns.Add(FormHelper.createCheckBoxColumn());
            dataView.Columns.Add(FormHelper.createTextColumn("userId", "userId", 100, false));
            dataView.Columns.Add(FormHelper.createTextColumn("账号", "loginName", 100, true));
            dataView.Columns.Add(FormHelper.createTextColumn("姓名", "name", 100, true));
            dataView.Columns.Add(FormHelper.createTextColumn("性别", "sex", 100, true));
            dataView.Columns.Add(FormHelper.createTextColumn("机构", "organ.name", 100, true));
            dataView.Columns.Add(FormHelper.createLinkColumn("Detail", "编辑", true));

            // 当前页为第一页
            this.pager.PageCurrent = 1;
            // 每页记录数
            this.pager.PageSize = 50;
            // 绑定
            this.pager.Bind();
        }

        /// <summary>
        /// 显示编辑页面
        /// </summary>
        public void showEdit(sys_user user = null)
        {
            FrmUserEdit formEdit = new FrmUserEdit(user);
            
            if (DialogResult.OK == formEdit.ShowDialog())
            {
                pager.Bind();
            }
        }


        /// <summary>
        /// 查询条件绑定
        /// </summary>
        public int queryBind()
        {
            // 分页设置
            PageInfo pagerInfo = new PageInfo()
            {
                CurrentPageIndex = pager.PageCurrent,
                PageSize = pager.PageSize,
                RecordCount = pager.PageCurrent * pager.PageSize
            };

            // 查询条件
            var where = LambdaHelper.True<sys_user>();
            if (query)
            {
                //whereStr = QueryHelper.GetWhereString(this.groupBox1); //获取自定义查询条件
            }

            UserBLL userBLL = new UserBLL();
            IList<sys_user> list = userBLL.FindWithPagerAndOrderby(where, ref pagerInfo, "createTime");
            //绑定分页控件   
            pager.bindingSource.DataSource = list;
            pager.bindingNavigator.BindingSource = pager.bindingSource;
            //分页控件绑定DataGridView   
            dataView.DataSource = pager.bindingSource;
            //返回总记录数   
            return pagerInfo.RecordCount;
        }

        private void FrmUser_Load(object sender, EventArgs e)
        {
            initView();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            showEdit();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            query = true;
            pager.Bind();
        }

        private void btnQueryAll_Click(object sender, EventArgs e)
        {
            query = false;
            pager.Bind();
        }

        private void gvPerson_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataView.Columns[e.ColumnIndex].Name == "Detail")
            {
                string strSelectedUserId = dataView.Rows[e.RowIndex].Cells["userId"].Value.ToString();
                UserBLL userBLL = new UserBLL();
                sys_user user = userBLL.FindSingle(LambdaHelper.True<sys_user>().And(p => strSelectedUserId.Equals(p.userId.ToString())));

                if (null == user)
                {
                    MessageBox.Show("未知错误");
                }
                else
                {
                    showEdit(user);
                }
            }          
        }

        private int pager_EventPaging()
        {
            return queryBind();
        }

        private EventPagingArg pager_EventExcel()
        {
            return new EventPagingArg("用户管理", this.dataView);
        }

        private EventPagingArg pager_EventDelItem()
        {
            return new EventPagingArg("删除用户", this.dataView);
        }
    }
}
